package com.example.enginetestandroid.widget;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.example.enginetestandroid.App;
import com.example.enginetestandroid.R;
import com.example.enginetestandroid.interfac.OnClickTabListener;

public class CommonBottomBar extends LinearLayout implements View.OnClickListener {

    public static final int HOME = 0;
    public static final int ME = 1;

    public Button tab_home, tab_me;

    private OnClickTabListener mOnClickTabListener;

    public CommonBottomBar(Context context) {
        super(context);
        initView(context);
    }

    public CommonBottomBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    private void initView(Context context) {
        LayoutInflater inflater = LayoutInflater.from(context);
        inflater.inflate(R.layout.view_bottombar, this);
        findViews(context);
    }

    private  void findViews(Context context) {
        tab_home = findViewById(R.id.tab_1);
        tab_home.setOnClickListener(this);

        tab_me = findViewById(R.id.tab_2);
        tab_me.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tab_1:
                if (mOnClickTabListener != null) {
                    mOnClickTabListener.onclick(HOME);
                }
                setHome();
                break;
            case R.id.tab_2:
                if (mOnClickTabListener != null) {
                    mOnClickTabListener.onclick(ME);
                }
                setMe();
                break;
            default:
                break;
        }
    }

    public void setHome() {
        setTabReset();
        Drawable dr = ContextCompat.getDrawable(App.Instance(),R.drawable.discover_selected);
        dr.setBounds(0, 0, dr.getMinimumWidth(), dr.getMinimumHeight());
        tab_home.setCompoundDrawables(null, dr, null, null);
        tab_home.setTextColor(ContextCompat.getColor(App.Instance(),R.color.colorBottomBar));
    }

    public void setMe() {
        setTabReset();
        Drawable dr = ContextCompat.getDrawable(App.Instance(),R.drawable.mine_selected);
        dr.setBounds(0, 0, dr.getMinimumWidth(), dr.getMinimumHeight());
        tab_me.setCompoundDrawables(null, dr, null, null);
        tab_me.setTextColor(ContextCompat.getColor(App.Instance(),R.color.colorBottomBar));
    }

    private void  setTabReset() {
        Drawable dr1 = ContextCompat.getDrawable(App.Instance(), R.drawable.discover_normal);
        dr1.setBounds(0,0, dr1.getMinimumWidth(), dr1.getMinimumHeight());
        tab_home.setCompoundDrawables(null, dr1, null, null);
        tab_home.setTextColor(ContextCompat.getColor(App.Instance(),R.color.black));

        Drawable dr2 = ContextCompat.getDrawable(App.Instance(), R.drawable.mine_normal);
        dr2.setBounds(0,0, dr2.getMinimumWidth(), dr2.getMinimumHeight());
        tab_me.setCompoundDrawables(null, dr2, null, null);
        tab_me.setTextColor(ContextCompat.getColor(App.Instance(),R.color.black));
    }

    public void setOnClickTabListener(OnClickTabListener OnClickTabListener) {
        this.mOnClickTabListener = OnClickTabListener;
    }
}
